/*
 * @lc app=leetcode.cn id=226 lang=javascript
 *
 * [226] 翻转二叉树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {TreeNode}
 */
var invertTree = function(root) {
   if(!root){return null}
   let queue=[root]
   while(queue.length){
       let cur=queue.shift()
       let temp=cur.left
       cur.left=cur.right
       cur.right=temp
       if(cur.left){
           queue.push(cur.left)
       }
       if(cur.right){
           queue.push(cur.right)
       }
   }
   return root//BFS
}
// @lc code=end

var invertTree = function(root) {
    if(root==null){return null}
    let left=root.left
    let right=root.right
    root.left=right
    root.right=left
    if(root.left){invertTree(root.left)}
    if(root.right){invertTree(root.right)}
    return root
}//DFS